package GUI_Data.Staff;

import Data_Flow.DataOperator;
import Data_Flow.GetData;
import GUI_Data.User.*;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
//
//import static com.sun.java.swing.ui.CommonUI.setButtonSize;\

/**
 * This class contains all the Button.
 * @author Chunxi Lin
 */
@Data
public class AllButtonStaff {

    private AllButtonStaff thisObject;
    private GetData getData;

    //4 for staff login
    private int loginMethod = 4;

    //3 for ID, 4 for password
    private int inputMethod = 0;

    private boolean ifCapsLock = false;

    private NewButton exitButton = new NewButton("Exit  ","red",2);

    private NewButton backToOrigin = new NewButton("     Origin","green",3);
    private NewButton backToDisplay = new NewButton("    Display","white",3);
    private NewButton lastButton = new NewButton(" Last page","grey",1);
    private NewButton nextButton = new NewButton(" Next page","grey",1);

    private JButton[] keyboard3;

    private JButton capsLock3  = new JButton("CapsClock");

    private JButton originDelete3 = new JButton("delete");

    private NewButton originConfirm = new NewButton("       confirm","grey",1);
    private NewButton finish = new NewButton("         finish","grey",1);

    /**
     * The generator of AllButton
     * Initialize all buttons on the login page.
     * @param staff The staff frame which contains this AllButton class.
     * @param dimension The dimension information.
     */
    public AllButtonStaff(Staff staff, SetDimension dimension) {

        setButtonSize(dimension);

        thisObject = this;

        backToDisplay.setEnabled(false);

        lastButton.addActionListener(staff);

        nextButton.addActionListener(staff);

        int index = 0;
        keyboard3 = new JButton[AllData.keyboardChar.length];

        for (char i : AllData.keyboardChar) {
            getKeyboard3()[index] = new JButton(String.valueOf(i));
            getKeyboard3()[index].setPreferredSize(dimension.getKeyboardDimension());
            index++;
        }
        capsLock3.setBackground(Color.WHITE);
    }

    /**
     * This method is used to add listening actions to all buttons.
     * This method is called when the window is initialized and reset.
     * @param staff The userFrame which contains this AllButton class.
     * @param dimension All the dimension information.
     * @param allPanel All the JPanel objects.
     * @param allLabelAndTextField All the JLabel and JTextField objects.
     * @param card The CardLayout object of the center panel.
     * @param login The CardLayout object of the input panel in login page.
     * @param passenger The CardLayout object of the passengerlist panel in login page.
     */
    public void addActionPerform(Staff staff, SetDimension dimension, AllPanelStaff allPanel, AllLabelAndTextFieldStaff allLabelAndTextField, CardLayout card, CardLayout login, CardLayout passenger){
        exitButton.addActionListener(e -> System.exit(0));
        backToOrigin.addActionListener(e -> card.show(allPanel.getCenterPanel(),"Origin page"));
//        backToDisplay.addActionListener(e -> toDisplayPage(allLabelAndTextField, allPanel, card));

        allLabelAndTextField.getNum().addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                inputMethod = 3;
            }

            @Override
            public void focusLost(FocusEvent e) {
            }
        });

        originConfirm.addActionListener(e -> {
            getData = new GetData(allLabelAndTextField.getNum().getText(),1);
            if (getData.isIfSuccessStaff()) {
                allLabelAndTextField.preInit(getData);
                allPanel.preInit(allLabelAndTextField);
                passenger.show(allPanel.getPassengerPanel(),"information");
                card.show(allPanel.getCenterPanel(), "Flight page");
                backToOrigin.setEnabled(false);
                backToDisplay.setBackground(Color.GREEN);

            } else {
                allLabelAndTextField.getInput_flightNum().setForeground(Color.RED);
                allLabelAndTextField.getInput_flightNum().setText("Not match! Please check your input:");
            }
        });

        originDelete3.addActionListener(e -> {
            if (allLabelAndTextField.getNum().getText() != null)
                allLabelAndTextField.getNum().setText(allLabelAndTextField.getNum().getText().substring(0, allLabelAndTextField.getNum().getText().length()-1));
        });

        for (int i = 0; i < keyboard3.length; i++) {
            int finalI = i;
            getKeyboard3()[i].addActionListener(e -> {
                if (ifCapsLock)
                    allLabelAndTextField.getNum().setText(allLabelAndTextField.getNum().getText() + AllData.keyboardChar[finalI]);
                else
                    allLabelAndTextField.getNum().setText(allLabelAndTextField.getNum().getText() + AllData.keyboardChar[finalI].toString().toLowerCase());
            });
        }
        capsLock3.addActionListener(e -> {
            if (ifCapsLock){
                capsLock3.setBackground(Color.WHITE);
                capsLock3.setForeground(Color.BLACK);
            }else {
//                capsLock3.setBackground(Color.BLACK);
                capsLock3.setBackground(Color.GRAY);
            }
            ifCapsLock = !ifCapsLock;
        });

        finish.addActionListener(e -> {
            card.show(allPanel.getCenterPanel(),"Origin page");
//            backToDisplay.setBackground(Color.GRAY);
//
//            allLabelAndTextField.getNum().setText("");
            staff.initStaff();
        });
    }

    /**
     * The method is used to set the dimension for all the button.
     * @param dimension All the dimension information.
     */
    private void setButtonSize(SetDimension dimension){

        exitButton.setPreferredSize(dimension.getNav_Exit());
        backToOrigin.setPreferredSize(dimension.getNav());
        backToDisplay.setPreferredSize(dimension.getNav());
        lastButton.setPreferredSize(dimension.getNav());
        nextButton.setPreferredSize(dimension.getNav());
        originConfirm.setPreferredSize(dimension.getSide_button());
        finish.setPreferredSize(dimension.getSide_button());
    }
}
